home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 46 / Amiga Format CD46 (1999-10-20)(Future Publishing)(GB)[!][issue 1999-12].iso / -serious- / programming / arexx / rxlistview / testrxlv.rexx < prev   
OS/2 REXX Batch file  |  1999-09-06  |  12KB  |  369 lines

  1. /*
  2. ** $VER: testrxlv.rexx 1.4 (12.8.99) Rolf Max Rotvel
  3. */
  4.  
  5. /*
  6. ** The listview uses functions from rexxsupport.library...
  7. */
  8. call addlib('rexxsupport.library', 0, -30, 0)
  9.  
  10. /*
  11. ** First set the variables that decide how big the listview should be.
  12. ** This must be done before rxlv_init() is called.
  13. */
  14. rxlv.width = 300
  15. rxlv.height = 200
  16.  
  17. /*
  18. ** Initialize the listview. This needs to be done only once. 
  19. */
  20. call rxlv_init()
  21.  
  22. /*
  23. ** Create a test stem. The stem must have the following format:
  24. **
  25. ** <stemname>.0 = number of elements
  26. ** <stemname>.1 = element no 1
  27. ** <stemname>.2 = element no 2
  28. ** ...
  29. */
  30. do i = 1 to 20
  31.     teststem.i = copies(i, i)
  32. end
  33. teststem.0 = 20
  34.  
  35. /*
  36. ** Now create the formatted stem, "viewline.", for the listview.
  37. ** rxlv_init() must have been called before this to create the
  38. ** rxlv.dispcols value.
  39. ** "viewline." is the only global variable besides "rxlv." that the
  40. ** listview procedures shares with the rest of the script.
  41. */
  42. do i = 1 to teststem.0
  43.     viewline.i = left(teststem.i, rxlv.dispcols)
  44. end
  45. viewline.0 = teststem.0
  46.  
  47. /*
  48. ** Loop while displaying the listview.
  49. **
  50. ** The syntax for the main listview procedure is:
  51. ** num = rxlv_main(<title text>, <inline keys>)
  52. **
  53. ** <title text> will be displayed in the window title.
  54. ** <inline keys> will be checked in addition to the normal keys. Which are
  55. ** (Shift)Cursor up/down, Return, Delete, Escape and Help.
  56. **
  57. ** <num> is the variable number of the highlighted item in the listview or 
  58. ** 0 if the list is empty. <num> is always returned no matter what key the
  59. ** user presses to leave the listview.
  60. **
  61. ** The variable rxlv.key contains either the exact inline key or one of the
  62. ** following:
  63. ** RET : User pressed Return
  64. ** DEL :              Delete
  65. ** ESC :              Escape
  66. **
  67. ** Note that the inline keys are case sensitive. Also note that you have to
  68. ** decide for yourself what to do when the user presses Return, Delete or
  69. ** Escape (And Help)
  70. **
  71. ** And note that you have to close the window yourself (By calling
  72. ** rxlv_closewin()). This ensures that the listview is only updated when you
  73. ** want it to.
  74. */
  75. do forever
  76.      num = rxlv_main('"q" or "ESC" quits ['viewline.0'] "HELP" for keys', 'qQ')   
  77.      say 'You pressed  : 'rxlv.key
  78.      say 'And selected : 'teststem.num
  79.  
  80.      select 
  81.         when upper(rxlv.key) = 'Q' | rxlv.key = 'ESC' then leave
  82.         when rxlv.key = 'DEL' then do
  83.             options prompt ' Delete it? Y/n '
  84.             pull ans
  85.             if ans ~= 'N' then do
  86.                 if teststem.0 > 0 then do
  87.                     do i = num to teststem.0
  88.                         next = i + 1
  89.                         teststem.i = teststem.next
  90.                         viewline.i = viewline.next
  91.                     end
  92.                     teststem.0 = teststem.0 - 1
  93.                     viewline.0 = teststem.0
  94.                 end
  95.                 call rxlv_closewin()
  96.             end
  97.         end
  98.         otherwise nop
  99.     end
  100. end
  101. exit
  102.  
  103.  
  104. /*
  105. ** In this procedure you decide what will happen when the user presses Help 
  106. ** in the listview. If you don't want anything to happen then just let it 
  107. ** return immediately. 
  108. */ 
  109. RXLV_HELP: procedure 
  110. say 'Use Cursor/Shift Cursor to' 
  111. say 'move and Enter to select.' 
  112. say 'Help gives you this text :-)'
  113. say 'Delete deletes an item in the list'
  114. say 'Escape or q exits the listview'
  115. return
  116.  
  117. /*
  118. ** Don't touch anything after the rxlv_help() procedures. Unless you know
  119. ** what you're doing of course :-) 
  120. */
  121. RXLV_MAIN: procedure expose viewline. rxlv.
  122. parse arg titletxt, inlinechars
  123.  
  124. /* Reset key */
  125. rxlv.key = ''
  126.  
  127. /* Which is bigger - win rows or lines in stemvar? */
  128. if rxlv.disprows > viewline.0 then rxlv.actrows = viewline.0
  129. else rxlv.actrows = rxlv.disprows
  130.  
  131. /* Get current mouse coordinates */
  132. if ~rxlv.opened? then do
  133.     call forbid()
  134.     mousex = c2d(import(offset(rxlv.screen, 18), 2)) - 50      /* Screen->MouseX */
  135.     mousey = c2d(import(offset(rxlv.screen, 16), 2)) - 50      /* Screen->MouseY */
  136.     call permit()
  137.  
  138.     /* Open the listview */
  139.     /* Open the listview */
  140.     if ~open(rxlv.win, 'RAW:'mousex'/'mousey'/'rxlv.width'/'rxlv.height'/'titletxt'/NOSIZE', 'w') then do
  141.         say 'Could not open listview window!'
  142.         exit 10
  143.     end
  144.     call writech(rxlv.win, rxlv.nocursor||rxlv.nowordwrap)
  145.     /* Initialize window */
  146.     if viewline.0 > 0 then do
  147.         rxlv.row = 1 
  148.         rxlv.var = 1
  149.         rxlv.topvar = 1 
  150.         call writech(rxlv.win, rxlv_getlighty(rxlv.row, rxlv.var)||rxlv.nl||rxlv_getpage(rxlv.var + 1))
  151.     end
  152.     rxlv.opened? = 1
  153. end
  154.  
  155. /* Do ze stuff */
  156. do forever
  157.     rxlv.oldrow = rxlv.row 
  158.     rxlv.oldvar = rxlv.var
  159.  
  160.     char = readch(rxlv.win, 1)
  161.     select
  162.         when char = rxlv.csi then do
  163.             char = readch(rxlv.win, 1)
  164.             select
  165.                 when viewline.0 < 2 then nop
  166.                 when char = rxlv.cursordown then do
  167.                     if rxlv.oldvar ~= viewline.0 then do
  168.                         line = rxlv_getunlighty()
  169.                         rxlv.var = rxlv.var + 1
  170.  
  171.                         if rxlv.oldrow < rxlv.actrows then rxlv.row = rxlv.row + 1 
  172.                         else do
  173.                             line = line||rxlv.nl
  174.                             rxlv.row = rxlv.actrows
  175.                             rxlv.topvar = rxlv.topvar + 1
  176.                         end
  177.                         call writech(rxlv.win, line||rxlv_getlighty())
  178.                     end
  179.                     else call rxlv_top()
  180.                 end  
  181.                 when char = rxlv.cursorup then do
  182.                     if rxlv.oldvar ~= 1 then do
  183.                         line = rxlv_getunlighty()
  184.                         rxlv.var = rxlv.var - 1
  185.  
  186.                         if rxlv.oldrow ~= 1 then do
  187.                             rxlv.row = rxlv.row - 1
  188.                             call writech(rxlv.win, line||rxlv_getlighty())
  189.                         end
  190.                         else do
  191.                             rxlv.row = 1 
  192.                             rxlv.topvar = rxlv.topvar - 1
  193.                             call writech(rxlv.win, line||rxlv_getlighty()||rxlv.nl||rxlv_getpage(rxlv.var + 1))
  194.                         end            
  195.                     end
  196.                     else call rxlv_bottom()                  
  197.                 end
  198.                 when char = rxlv.scursorup then do
  199.                     if rxlv.oldvar ~= 1 then do
  200.                         rxlv.row = 1
  201.                         rxlv.var = rxlv.topvar
  202.  
  203.                         if rxlv.oldrow = 1 then do
  204.                             if rxlv.oldvar - rxlv.actrows < 1 then rxlv.topvar = 1
  205.                             else rxlv.topvar = rxlv.oldvar - rxlv.actrows
  206.                             rxlv.var = rxlv.topvar
  207.                             call writech(rxlv.win, rxlv.cls||rxlv_getlighty()||rxlv.nl||rxlv_getpage(rxlv.topvar + 1))
  208.                         end
  209.                         else call writech(rxlv.win, rxlv_getunlighty()||rxlv_getlighty())
  210.                     end
  211.                     else call rxlv_bottom()                  
  212.                 end
  213.                 when char = rxlv.scursordown then do
  214.                     if rxlv.oldvar ~= viewline.0 then do
  215.                         rxlv.row = rxlv.actrows
  216.  
  217.                         if rxlv.oldrow = rxlv.actrows then do
  218.                             if rxlv.oldvar + rxlv.actrows > viewline.0 then rxlv.topvar = viewline.0 - (rxlv.actrows - 1)
  219.                             else rxlv.topvar = rxlv.oldvar + 1
  220.                             rxlv.var = min(viewline.0, rxlv.topvar + (rxlv.actrows - 1))
  221.                             call writech(rxlv.win, rxlv.cls||rxlv_getpage(rxlv.topvar)||rxlv.nl||rxlv_getlighty())
  222.                         end
  223.                         else do
  224.                             rxlv.var = (rxlv.topvar + rxlv.actrows) - 1
  225.                             call writech(rxlv.win, rxlv_getunlighty()||rxlv_getlighty())
  226.                         end
  227.                     end
  228.                     else call rxlv_top()
  229.                 end
  230.                 otherwise nop
  231.             end
  232.         end
  233.         when char = rxlv.esc then do
  234.             rxlv.key = 'ESC'
  235.             return rxlv_val()
  236.         end
  237.         when char = rxlv.cr then do
  238.             rxlv.key = 'RET'
  239.             return rxlv_val()
  240.         end
  241.         when char = rxlv.del then do
  242.             rxlv.key = 'DEL'
  243.             return rxlv_val()
  244.         end
  245.         when pos(char, inlinechars) > 0 then do
  246.             rxlv.key = char
  247.             return rxlv_val()
  248.         end
  249.         when char = rxlv.help then call rxlv_help()
  250.         otherwise nop
  251.     end
  252. end
  253.  
  254.  
  255. RXLV_VAL: procedure expose rxlv. viewline.
  256. if viewline.0 = 0 then return 0
  257. return rxlv.oldvar
  258.  
  259.  
  260. RXLV_TOP: procedure expose rxlv. viewline.
  261. rxlv.var = 1
  262. rxlv.row = 1
  263.  
  264. if rxlv.topvar = 1 then do   /* Just move to top */
  265.     line = rxlv_getunlighty()
  266.     call writech(rxlv.win, line||rxlv_getlighty())
  267. end
  268. else do
  269.     rxlv.topvar = 1
  270.     call writech(rxlv.win, rxlv.cls||rxlv_getlighty()||rxlv.nl||rxlv_getpage(rxlv.var + 1))
  271. end
  272. return
  273.  
  274.  
  275. RXLV_BOTTOM: procedure expose rxlv. viewline.
  276. rxlv.var = viewline.0
  277.  
  278. if viewline.0 <= rxlv.actrows then do 
  279.     line = rxlv_getunlighty()
  280.     rxlv.row = viewline.0
  281.     call writech(rxlv.win, line||rxlv_getlighty())
  282. end
  283. else do
  284.     rxlv.row = rxlv.actrows
  285.     rxlv.topvar = (viewline.0 - rxlv.actrows) + 1
  286.     call writech(rxlv.win, rxlv.cls||rxlv_getpage(rxlv.topvar)||rxlv.nl||rxlv_getlighty())
  287. end
  288. return
  289.  
  290.  
  291. RXLV_GETPAGE: procedure expose viewline. rxlv.
  292. if viewline.0 = 1 then return ''
  293.  
  294. top = arg(1)
  295. page = ''
  296. do y = 1 to rxlv.actrows - 2                    /* Lines between first and last */
  297.     page = page||viewline.top||rxlv.nl
  298.     top = top + 1
  299. end 
  300. page = page||viewline.top                       /* No newline after last line */
  301. return page
  302.  
  303.  
  304. RXLV_GETUNLIGHTY: procedure expose rxlv. viewline. 
  305. var = rxlv.oldvar
  306. return rxlv.csi||rxlv.oldrow'H'viewline.var
  307.  
  308.  
  309. RXLV_GETLIGHTY: procedure expose rxlv. viewline. 
  310. var = rxlv.var
  311. return rxlv.csi||rxlv.row'H'rxlv.hilite||viewline.var||rxlv.off
  312.  
  313.  
  314. RXLV_CLOSEWIN: procedure expose rxlv.
  315. if rxlv.opened? then do
  316.     call close(rxlv.win)
  317.     rxlv.opened? = 0
  318. end
  319. return 
  320.  
  321.  
  322. RXLV_INIT: procedure expose rxlv.
  323. /* Hardcoded minimum values */
  324. rxlv.width = max(100, rxlv.width)
  325. rxlv.height = max(50, rxlv.height)
  326.  
  327. /* ANSI stuff */
  328. rxlv.csi = '9b'x  ; rxlv.esc = '1b'x
  329. rxlv.help = '7e'x ; rxlv.del = '7f'x
  330. rxlv.nl = '0a'x   ; rxlv.cr = '0d'x
  331. rxlv.off = rxlv.csi||'0m' 
  332. rxlv.topleft = rxlv.csi'48'x 
  333. rxlv.cls = rxlv.csi'H'rxlv.csi'J'
  334. rxlv.hilite = rxlv.csi'43;32m'
  335. rxlv.nowordwrap = rxlv.csi||'3f376c'x
  336. rxlv.nocursor = rxlv.csi||'302070'x 
  337. rxlv.cursorup = '41'x  ; rxlv.cursordown = '42'x 
  338. rxlv.scursorup = '54'x ; rxlv.scursordown = '53'x
  339. rxlv.win = 'listwin'
  340.  
  341. /* GUI constants */
  342. guiheight = 7 ; guiwidth = 8
  343.  
  344. /* Font info */
  345. intui = showlist(l, 'intuition.library',, a)
  346. call forbid()
  347. rxlv.screen = next(intui, 56)               /* IntuitionBase->ActiveScreen */
  348. font = next(rxlv.screen, 136)               /* Screen->RastPort.Font */
  349. fonty = c2d(import(offset(font, 20), 2))    /* Font->YSize */
  350. fontx = c2d(import(offset(font, 24), 2))    /* Font->XSize */
  351. call permit()
  352.  
  353. /* Listview width */
  354. do while (rxlv.width - guiwidth) // fontx ~= 0 
  355.     rxlv.width = rxlv.width + 1 
  356. end
  357. rxlv.dispcols = ((rxlv.width - guiwidth) % fontx)
  358. rxlv.filler = copies(' ', rxlv.dispcols)
  359.  
  360. /* Listview height */
  361. const = guiheight + fonty
  362. do while (rxlv.height - const) // fonty ~= 0 
  363.     rxlv.height = rxlv.height + 1 
  364. end
  365. rxlv.disprows = (rxlv.height - const) % fonty
  366.  
  367. rxlv.opened? = 0
  368. return
  369.